package com.hs.fm;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import com.chiemy.cardview.R;
import com.iflytek.voiceads.AdError;
import com.iflytek.voiceads.AdKeys;
import com.iflytek.voiceads.IFLYAdListener;
import com.iflytek.voiceads.IFLYAdSize;
import com.iflytek.voiceads.IFLYFullScreenAd;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class FMControlActivity extends FragmentActivity {

	private ArrayList<Fragment> fragments;

	private ViewPager viewPager;

	private TextView tab_color_cntl;

	private TextView tab_music;

	private int line_width;

	private View line;

	private IFLYFullScreenAd iv;
	private boolean isBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fm_activity_layout);

		tab_color_cntl = (TextView) findViewById(R.id.tab_color_cntl);
		tab_music = (TextView) findViewById(R.id.tab_music);
		line = findViewById(R.id.line);
		initData();

		// 初始化TextView动画
		ViewPropertyAnimator.animate(tab_music).scaleX(1.2f).setDuration(0);
		ViewPropertyAnimator.animate(tab_music).scaleY(1.2f).setDuration(0);

		fragments = new ArrayList<Fragment>();
		fragments.add(new MusicFragment());
		fragments.add(new FMCntrlFrangment());
		line_width = getWindowManager().getDefaultDisplay().getWidth()
				/ fragments.size();
		line.getLayoutParams().width = line_width;
		line.requestLayout();

		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setAdapter(new FragmentStatePagerAdapter(
				getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return fragments.size();
			}

			@Override
			public Fragment getItem(int arg0) {
				return fragments.get(arg0);
			}
		});

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				changeState(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				float tagerX = arg0 * line_width + arg2 / fragments.size();
				ViewPropertyAnimator.animate(line).translationX(tagerX)
						.setDuration(0);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

		tab_color_cntl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(1);
				//initData();
			}
		});

		tab_music.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				viewPager.setCurrentItem(0);
			}
		});
	}

	@Override
    protected void onDestroy() {
        super.onDestroy();
        
    }
	
	 @Override
	 protected void onStop() {
		 super.onStop();
	 }
	/* 根据传入的值来改变状态 */
	private void changeState(int arg0) {
		if (arg0 == 0) {
			tab_music.setTextColor(getResources().getColor(R.color.green));
			tab_color_cntl.setTextColor(getResources().getColor(R.color.gray_white));
			ViewPropertyAnimator.animate(tab_music).scaleX(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(tab_music).scaleY(1.2f).setDuration(200);
			ViewPropertyAnimator.animate(tab_color_cntl).scaleX(1.0f)
					.setDuration(200);
			ViewPropertyAnimator.animate(tab_color_cntl).scaleY(1.0f)
					.setDuration(200);

		} else {
			tab_color_cntl.setTextColor(getResources().getColor(R.color.green));
			tab_music.setTextColor(getResources().getColor(R.color.gray_white));
			ViewPropertyAnimator.animate(tab_music).scaleX(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(tab_music).scaleY(1.0f).setDuration(200);
			ViewPropertyAnimator.animate(tab_color_cntl).scaleX(1.2f)
					.setDuration(200);
			ViewPropertyAnimator.animate(tab_color_cntl).scaleY(1.2f)
					.setDuration(200);
		}
	}

	private void initData() {
		iv = IFLYFullScreenAd.createFullScreenAd(this,
				"D32E34AE66DA0F27C83A94ED6E42A1B4");
		iv.setAdSize(IFLYAdSize.FULLSCREEN);
		iv.setParameter(AdKeys.SHOW_TIME_FULLSCREEN, "3000");
		iv.setParameter(AdKeys.DOWNLOAD_ALERT, "true");
		iv.loadAd(new IFLYAdListener() {

			@Override
			public void onAdReceive() {
				iv.showAd();
			}

			@Override
			public void onAdFailed(AdError arg0) {
				Toast.makeText(
						getApplicationContext(),
						arg0.getErrorCode() + "****"
								+ arg0.getErrorDescription(), 0).show();
				isBack = true;
			}

			@Override
			public void onAdClose() {
				isBack = true;
			}

			@Override
			public void onAdClick() {

			}

		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(MusicFragment.mediaPlayer.isPlaying()){
			MusicFragment.mediaPlayer.stop();
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isBack) {
				return super.onKeyDown(keyCode, event);
			} else {
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);

	}
}
